<script>
export default {
  name: "ScrollToReveal",
  mounted() {
    window.addEventListener('scroll', () => {
      let boxes = document.querySelectorAll('.box')
      boxes.forEach(item => {
        if (item.offsetTop < window.scrollY) {
          item.classList.add('active')
        } else {
          item.classList.remove('active')
        }
      })
    })
  }
}
</script>

<template>
  <div class="demo">
    <section>
      <h2>Scroll To Reveal</h2>
    </section>
    <div class="container">
      <div class="box" v-for="n in 60" :key="n"></div>
    </div>
  </div>
</template>

<style scoped lang="less">
.demo {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  background: #111;
  overflow-x: hidden;

  section {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;

    h2 {
      position: relative;
      color: #fff;
      font-size: 8vw;
      font-weight: 500;
    }
  }

  .container {
    position: relative;
    top: -200px;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    width: 700px;
    grid-gap: 30px;

    .box {
      top: 50vh;
      position: relative;
      width: 200px;
      height: 200px;
      border-radius: 10px;
      transition: 0.5s;
      background: #ffffff;
    }

    .box:nth-child(3n + 1) {
      transform: translate(-400px, 0) scale(0);
    }

    .box:nth-child(3n + 2) {
      transform: translate(0, 400px) scale(0);
    }

    .box:nth-child(3n + 3) {
      transform: translate(400px, 0) scale(0);
    }

    .box.active {
      transform: translate(0, 0) scale(1);
    }
  }
}
</style>